--- 模块功能：发送设备位置到地震预警管理终端系统
-- @author zhangfenghe
-- @module LOC.sendDeviceLocation
-- @license MIT
-- @copyright openLuat
-- @release 2022.03.29
module(..., package.seeall)
require "getLbsLoc"
require "nvm"
require "logGenerator"
require "mqttOutMsg"
nvm.init("config.lua", true)
local slen = string.len

-- 预警设备参数设置-身份判别
local function containClientsOrNot(clients)
    do
        local containResult = false
        for i = 1, #clients do
            if clients[i] == nvm.get("info")[1] then
                containResult = true
            end
        end
        log.info("containResult", containResult)
        return containResult
    end
end

-- 发送设备位置信息到后端系统
function seviceLocationSendTask(tjsondata)
    local clients, logInfo = tjsondata["clients"], ""
    if containClientsOrNot(clients) then
        local topic = "action"
        local sendMsg = "{" .. "\"todo\":\"gps\",\"clientNo\":\"" ..
            nvm.get("info")[1] .. "\",\"lon\":" .. getLbsLoc.Lng ..
            ",\"lat\":" .. getLbsLoc.Lat .. "}"
        sendMsg = crypto.aes_encrypt("ECB", "PKCS5", sendMsg, "keson-123abcdefg")
        sendMsg = crypto.base64_encode(sendMsg, slen(sendMsg))
        --发布设备位置到后台
        mqttOutMsg.insertMsg(topic, sendMsg, 0)
        if logInfo ~= nil and logInfo ~= false then
            logGenerator.generateLogTask(logInfo, "log")
        end
        return true
    else
        return false
    end
end
